<?php

# LemurCMS by Alec Gorge
#
#     This file contains the PHP functions used throught the CMS.
#
# updated: Feb. 6, 2009

class themeManager extends functions {
	public $nav_link, $r, $staticContent;
	public function __construct ($id = "") {
		$this->id = $id;
		$this->arrayStaticContent();
	}
	public function arrayStaticContent () {
		if(!isset($GLOBALS['staticContent'])) {
			$sql = "SELECT * FROM ".DBTABLEPREFIX."content_template WHERE template = '".$this->getSetting('Theme')."' ORDER by id";
			$run = $this->db_query($sql)or die(mysql_error());
				$call_info = array_shift( debug_backtrace() );
				$code_line = $call_info['line'];
				$file = array_pop( explode('/', $call_info['file']));
			$GLOBALS['settingsDebug'] .= 'function called on line: '.$code_line . " of ".basename($file)."<br/>";
			while($rec = mysql_fetch_assoc($run)) {
				$r[$rec['name']] = array(
					'id' => $rec['id'],
					'content' => stripslashes($rec['content']),
					'name' => $rec['name'],
					'textOnly' => $rec['textOnly']
				);
			}
			$GLOBALS['staticContent'] = $r;
		}
		return $GLOBALS['staticContent'];
	}
	public function arrayNavigation($oneline = false) {
		if($oneline == false) {
			$refs = array();
			$list = array();
			$sql = "SELECT parent_id, menutitle, id FROM ".DBTABLEPREFIX."content_pages WHERE NOT secondary = 'yes' ORDER by orderingid ";
			$run = $this->db_query($sql)or die(mysql_error());
			while($data = @mysql_fetch_assoc($run)) {
				$thisref = &$refs[ $data['id'] ];
				$thisref['id'] = $data['id'];
				if ($data['parent_id'] == 0) {
					$list[ $data['menutitle'] ] = &$thisref;
				} else {
					$refs[ $data['parent_id'] ]['children'][ $data['menutitle'] ] = &$thisref;
				}
			}
			return $list;
		}
		else {
			$sql = "SELECT * FROM ".DBTABLEPREFIX."content_pages WHERE NOT secondary = 'yes' ORDER by orderingid ";
			$run = $this->db_query($sql)or die(mysql_error());
			while($rec = mysql_fetch_assoc($run)) {
				$return[$rec['menutitle']] = $rec['id'];	
			}
			return $return;
		}
	}
	public function arraySecondaryNavigation($oneline = false) {
		if($oneline == false) {
			$refs = array();
			$list = array();
			$sql = "SELECT parent_id, menutitle, id FROM ".DBTABLEPREFIX."content_pages WHERE secondary = 'yes' ORDER by orderingid";
			$run = $this->db_query($sql)or die(mysql_error());
			while($data = @mysql_fetch_assoc($run)) {
				$thisref = &$refs[ $data['id'] ];
				$thisref['id'] = $data['id'];
				if ($data['parent_id'] == 0) {
					$list[ $data['menutitle'] ] = &$thisref;
				} else {
					$refs[ $data['parent_id'] ]['children'][ $data['menutitle'] ] = &$thisref;
				}
			}
			return $list;
		}
		else {
			$sql = "SELECT * FROM ".DBTABLEPREFIX."content_pages WHERE secondary = 'yes' ORDER by orderingid";
			$run = $this->db_query($sql)or die(mysql_error());
			while($rec = mysql_fetch_assoc($run)) {
				$return[$rec['menutitle']] = $rec['id'];	
			}
			return $return;
		}
	}
	public function optionListFromArray($arr, $pre, $selected = "", $indent=0, $mother_run=true, $nameInValue = 0){
		$GLOBALS['cleanUrls'] = $this->getSetting('Clean Urls');
		foreach($arr as $k=>$v){
			// skip the baseval thingy. Not a real node.
			if($k == "__base_val") continue;
			
			if($k == "id") continue;
			// determine the real value of this node.
			$show_val = ( is_array($v) ? $v["__base_val"] : $v );
	 
			if($k == "children") {
				$return .= $this->optionListFromArray($v, $pre, $selected, ($indent), false, $nameInValue);			
			}
			else {
				// show the actual node
				if($nameInValue == 1) {
					if($GLOBALS['cleanUrls'] == "Enabled") {
						$return .= "\t".str_repeat("\t", $indent)."<option value='".S_BASE_URL."content/".make_clean_url($k)."/".$v['id']."'";
					}
					else {
						$return .= "\t".str_repeat("\t", $indent)."<option value='".S_BASE_URL."?page=".$v['id']."'";					
					}
				}
				else {
					$return .= "\t".str_repeat("\t", $indent)."<option value='".$pre.$v['id']."'";
				}
				if($selected != "") {
					if($selected == $v['id']) {
						$return .= " selected='selected'";
					}
				}
				$return .= ">".str_repeat("&#8212; ", $indent).stripslashes(stripslashes(stripslashes($k)))."</option>\n";
		 
				if(is_array($v)){
					// this is what makes it recursive, rerun for childs
					$return .= $this->optionListFromArray($v, $pre, $selected, ($indent+1), false, $nameInValue);
				}
			}
		}
		return $return;
	}
	public function buildNavArray($oneline = false) {
		return $this->arrayNavigation($oneline);
	}
	public function navigation ($ul_id ="ul-navigation",$sub_ul_class ="ul-sub-navigation",$lis = "selected",$link = true,$child = false,$inputArray = array()) {
		$GLOBALS['cleanUrls'] = $this->getSetting('Clean Urls');
		$GLOBALS['pageid'] = $_GET['page'];
	if($inputArray == array()) {
		$inputArray = $this->buildNavArray();
	}
		if($child == true) {
			$r .= "<ul class='$sub_ul_class'>";
		}
		else {
			$r .= "<ul id='$ul_id'>";
		}
		$x = 0;
		foreach($inputArray as $k=>$v) {
			if(is_array($v['children'])) {
				$r .= "\n\t<li id='item_$x' class='parent";
				if($GLOBALS['pageid'] == $v['id']) {
					$r .= " $lis";
				}
				$r .= "'>";
				if($link == true) {
					if($GLOBALS['cleanUrls'] == "Enabled") {
						$r .= "<a href='".THEME_URL."content/".make_clean_url($k)."/".$v['id']."'>";
					}
					else {
						$r .= "<a href='".THEME_URL."?page=".$v['id']."'>";					
					}
				}
				$r .= stripslashes(stripslashes(stripslashes($k)));
				if($link == true) {
					$r .= "</a>";
				}
				$r .= $this->navigation($ul_id, $sub_ul_class, $lis, $link, true,$v['children']);
				$r .= "</li>";
			}
			else {
				$r .= "\n\t<li id='item_$x'";
				if($GLOBALS['pageid'] == $v['id']) {
					$r .= " class='$lis'";
				}
				$r .= ">";
				if($link == true) {
					if($GLOBALS['cleanUrls'] == "Enabled") {
						$r .= "<a href='".THEME_URL."content/".make_clean_url($k)."/".$v['id']."'>";
					}
					else {
						$r .= "<a href='".THEME_URL."?page=".$v['id']."'>";					
					}
				}
				$r .= stripslashes(stripslashes(stripslashes($k)));
				if($link == true) {
					$r .= "</a>";
				}
				$r .= '</li>';
				$x++;
			}
		}
		$r .= "</ul>";
		return $r;
	}
	public function secondary_navigation ($ul_id ="ul-navigation",$sub_ul_class ="ul-sub-navigation",$lis = "selected",$link = true,$child = false,$inputArray = array()) {
		$GLOBALS['cleanUrls'] = $this->getSetting('Clean Urls');
		$GLOBALS['pageid'] = $_GET['page'];
	if($inputArray == array()) {
		$inputArray = $this->arraySecondaryNavigation();
	}
		if($child == true) {
			$r .= "<ul class='$sub_ul_class'>";
		}
		else {
			$r .= "<ul id='$ul_id'>";
		}
		$x = 0;
		foreach($inputArray as $k=>$v) {
			if(is_array($v['children'])) {
				$r .= "\n\t<li id='item_$x' class='parent";
				if($GLOBALS['pageid'] == $v['id']) {
					$r .= " $lis";
				}
				$r .= "'>";
				if($link == true) {
					if($GLOBALS['cleanUrls'] == "Enabled") {
						$r .= "<a href='".THEME_URL."content/".make_clean_url($k)."/".$v['id']."'>";
					}
					else {
						$r .= "<a href='".THEME_URL."?page=".$v['id']."'>";					
					}
				}
				$r .= stripslashes(stripslashes(stripslashes($k)));
				if($link == true) {
					$r .= "</a>";
				}
				$r .= $this->navigation($ul_id, $sub_ul_class, $lis, $link, true,$v['children']);
				$r .= "</li>";
			}
			else {
				$r .= "\n\t<li id='item_$x'";
				if($GLOBALS['pageid'] == $v['id']) {
					$r .= " class='$lis'";
				}
				$r .= ">";
				if($link == true) {
					if($GLOBALS['cleanUrls'] == "Enabled") {
						$r .= "<a href='".THEME_URL."content/".make_clean_url($k)."/".$v['id']."'>";
					}
					else {
						$r .= "<a href='".THEME_URL."?page=".$v['id']."'>";					
					}
				}
				$r .= stripslashes(stripslashes(stripslashes($k)));
				if($link == true) {
					$r .= "</a>";
				}
				$r .= '</li>';
				$x++;
			}
		}
		$r .= "</ul>";
		return $r;
	}
	public function static_content ($editable, $default, $textOnly = '') {
		$GLOBALS['cleanUrls'] = $this->getSetting('Clean Urls');
		$GLOBALS['pageid'] = $_GET['page'];
		if(isset($GLOBALS['staticContent'][$editable])) {
			$rec['content'] = $GLOBALS['staticContent'][$editable]['content'];
		}
		else {
			$sql = "INSERT INTO ".DBTABLEPREFIX."content_template (name, content, template, textOnly) VALUES ('".mysql_real_escape_string($editable)."', '".mysql_real_escape_string($default)."', '".$this->getSetting('Theme')."', '".mysql_real_escape_string($textOnly)."')";
			$run = $this->db_query($sql) or die(mysql_error());
			$rec['content'] = $default;
		}
		$r = stripslashes(stripslashes(stripslashes($rec['content'])));
		return $r;
	}
	public function buildNavigation () {
		
	}
}
?>